package com.zj.httpdemo.http.get;

import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zj.httpdemo.bean.UserBean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

/**
 * Created by Lenovo on 2017/5/13.
 */

public class JsonTask extends AsyncTask<String, Void, Void> {
    private static final String TAG = JsonTask.class.getSimpleName();

    @Override
    protected Void doInBackground(String... params) {
        Log.e(TAG, "doInBackground: url = " + params[0]);

        HttpsURLConnection connection = null;
        InputStream in = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            URL url = new URL(params[0]);

            connection = (HttpsURLConnection) url.openConnection();
            // Timeout for reading InputStream arbitrarily set to 3000ms.
            connection.setReadTimeout(3000);
            // Timeout for connection.connect() arbitrarily set to 3000ms.
            connection.setConnectTimeout(3000);
            // For this use case, set HTTP method to GET.
            connection.setRequestMethod("GET");
            // Already true by default but setting just in case; needs to be true since this request
            // is carrying an input (response) body.
            connection.setDoInput(true);

            // Open communications link (network traffic occurs here).
            connection.connect();

            Log.e(TAG, "doInBackground: response code = " + connection.getResponseCode());
            if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
                in = connection.getInputStream();
                isr = new InputStreamReader(in, "UTF-8");
                br = new BufferedReader(isr);

                StringBuffer sb = new StringBuffer();
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                Log.e(TAG, "doInBackground: sb = " + sb.toString());

                Gson gson = new Gson();
                List<UserBean> list = gson.fromJson(sb.toString(), new TypeToken<List<UserBean>>() {
                }.getType());

                for (UserBean user : list) {
                    Log.e(TAG, "doInBackground: " + user.toString());
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            connection.disconnect();
            try {
                if (in != null) {
                    in.close();
                }
                if (isr != null) {
                    isr.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        Log.e(TAG, "doInBackground: end");
        return null;
    }
}
